import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    curRouter: '',
    autoslide: {},
    headerInfo: {
      name: '',
      weatherInfo: {
        weatherType: '',
        weartherContent: '',
        temperature: ''
      },
      show: false
    },
    map: null,
    pagemap: {},
    pagehome: {},
    pagedetail: {},
    pagenews: {},
    exitUrl: ''
  },
  mutations: {
    setAutoSlide (state, obj) {
      const autoSlide = Object.assign(state.autoslide, obj)
      state.autoslide = autoSlide
      // console.log('obj', obj, state, autoSlide)
    },
    setCurRouter (state, curRouter) {
      state.curRouter = curRouter
    },
    setHeader (state, headerInfo) {
      state.headerInfo = headerInfo
    },
    setExitUrl (state, url) {
      state.exitUrl = url
    },
    setDetail (state, res) {
      state.pagedetail = res
    },
    setMap (state, res) {
      state.pagemap = res
    },
    setNews (state, res) {
      state.pagenews = res
    },
    setHome (state, res) {
      state.pagehome = res
    },
    setHeaderInfo (state, headerInfo) {
      state.headerInfo = headerInfo
    }
  },
  actions: {
    // 获取自动关东参数
    async getAutoSlide ({ commit, state }, data) {
      const res = await api.autoslide(data)
      commit('setAutoSlide', res)
    },
    async sendData ({ commit, state }, data) {
      await api.autoslide(data.query)
    },
    async getExitUrl ({ commit, state }, data) {
      const res = await api.exit(data)
      // console.log('resUrl', res)
      commit('setExitUrl', res.url)
    },
    async sendPopup ({ commit, state }, data) {
      await api.mapPopup(data)
    },
    async getDetail ({ commit, state }, data) {
      const res = await api.detail(data)
      commit('setDetail', res)
    },
    async getMap ({ commit, state }, data) {
      const res = await api.map(data)
      commit('setMap', res)
    },
    async getNews ({ commit, state }, data) {
      const res = await api.news(data)
      commit('setNews', res)
    },
    async getHome ({ commit, state }, data) {
      const res = await api.home(data)
      commit('setHome', res)
    }

  }
})
